package com.wh.myrealm;

import com.wh.dao.UserMapper;
import com.wh.pojo.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;

import java.sql.SQLException;

/**
 * @Auther: HuiEr
 * @Date: 2020/2/25 21:43
 * @Description
 */
public class MyRealm extends AuthenticatingRealm {
    private UserMapper userMapper=new UserMapper();
    public String getRealmName(){
        return "MyRealm";
    }
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       /* User huihuier = new User(1, "huihuier", "666");
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(huihuier.getUserName(), huihuier.getPassword(), getRealmName());
        return simpleAuthenticationInfo;*/

        String webUsername = (String)authenticationToken.getPrincipal();
        User user=null;
        try {
             user= userMapper.findUserByName(webUsername);
        } catch (Exception e) {
            System.out.println("查询失败"+e.fillInStackTrace());
        }
        if (null == user){
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getUserName(), user.getPassword(), getName());
        return simpleAuthenticationInfo;
    }
}
